package com.yanqu.road.server.gameplayer.module.cricket.battle;


import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.gameplayer.module.cricket.CricketMgr;
import com.yanqu.road.server.gameplayer.module.cricket.model.CricketBattleData;
import com.yanqu.road.server.gameplayer.module.cricket.model.CricketGameResult;
import com.yanqu.road.server.gameplayer.module.cricket.model.CricketMatchUserData;

import java.util.*;

public class CricketBattle {


    private Random random = new Random();


    private CricketMatchUserData matchUserData;

    private CricketMatchUserData matchUserData1;
    private long timestamp;

    public static CricketBattle getInstance(CricketMatchUserData matchUserData, CricketMatchUserData matchUserData1) {
        return new CricketBattle(matchUserData, matchUserData1);
    }

    public CricketBattle(CricketMatchUserData matchUserData, CricketMatchUserData matchUserData1) {

        this.matchUserData = matchUserData;
        this.matchUserData1 = matchUserData1;
        this.timestamp = System.currentTimeMillis();
    }

    public CricketBattle() {

    }

    public CricketProto.CricketBattleLog.Builder battle() {
        CricketProto.CricketBattleLog.Builder battleLogBuilder = CricketProto.CricketBattleLog.newBuilder();
        battleLogBuilder.setTimestamp(System.currentTimeMillis());

        CricketProto.CricketMatchRound.Builder matchRoundGroup = this.matchRound(this.matchUserData, this.matchUserData1);
        battleLogBuilder.setMatchRound(matchRoundGroup);
        return battleLogBuilder;
    }




    /**
     * 玩家对玩家
     *
     * @param cricketMatchUserData
     * @param cricketMatchUserData1
     * @return
     */
    public CricketProto.CricketMatchRound.Builder matchRound(CricketMatchUserData cricketMatchUserData, CricketMatchUserData cricketMatchUserData1) {

        CricketProto.CricketMatchRound.Builder matchRoundGroupBuilder = CricketProto.CricketMatchRound.newBuilder();
        matchRoundGroupBuilder.addUserBattleData(cricketMatchUserData.getUserBattleData());
        matchRoundGroupBuilder.addUserBattleData(cricketMatchUserData1.getUserBattleData());
        int winNum = 0;
        for (int i = 0; i < cricketMatchUserData.getCricketDataList().size(); i++) {
            CricketBattleData cricketTempMsg = cricketMatchUserData.getCricketDataList().get(i);
            CricketBattleData cricketTempMsg1 = cricketMatchUserData1.getCricketDataList().get(i);
            CricketGameResult cricketGameResult = this.gameRound(cricketTempMsg, cricketTempMsg1);
            CricketProto.CricketGameRound.Builder gameRound = cricketGameResult.getCricketGameRoundBuilder();

            if (gameRound.getWinUserId() == cricketMatchUserData.getUserId()) {
                winNum++;
            }
            matchRoundGroupBuilder.addGameRound(gameRound);
        }
        int cricketFightNumMax = GameConfig.CRICKET_FIGHT_DEF_TEAM_NUM;
        if (winNum > 1) {
            matchRoundGroupBuilder.setWinUserId(cricketMatchUserData.getUserId());
        } else {
            matchRoundGroupBuilder.setWinUserId(cricketMatchUserData1.getUserId());
        }
        matchRoundGroupBuilder.setScore(String.format("%d:%d", winNum, cricketFightNumMax - winNum));

        return matchRoundGroupBuilder;
    }


    /**
     * 蛐蛐对蛐蛐
     *
     * @param cricket
     * @param cricket1
     * @return
     */
    public CricketGameResult gameRound(CricketBattleData cricket, CricketBattleData cricket1) {
        CricketGame cricketGame = CricketGame.getInstance(cricket, cricket1);
        return cricketGame.result();
    }

}
